<?php 
namespace XFCore\traits\api;
/*
*app配置
*/
trait AppTrait{
    public function app_cron(){
        $token = $this->input->token;
        if(empty($token)){
            exitjson(-1,'token不可为空');
        }
        $usertoken = $this->decode_token($token);
        if(empty($usertoken['pid'])){
            exitjson(-1,'token信息错误！');
        }
        $userrow = $this->db->userrow($usertoken['pid'],'pid',"`pid`,`key`");
        if(empty($userrow) || $userrow['key'] != $usertoken['key']){
            exitjson(-1,'用户不存在');
        }
        $qr_row = $this->db->select('qrlist',"id",['pid'=>$userrow['pid'] , 'hook_type'=>1,'xf_type'=>3] ,['single_row'=>true,'limit'=>1]);
        if($qr_row){
            $timess=time() + 30;
            $this->db->update('qrlist',['status'=>1,'crontime'=>$timess , 'cookie'=>base64_encode('xfappguajicookie')],['hook_type'=>1,'pid'=>$userrow['pid'] , 'xf_type'=>3]);
        }
        exitjson(200,'Cron OK');
    }
    
    public function app_notify($get , $post , $data){
        $data = empty($data)?$get:$data;
        $requiredFields = ['money', 'type', 'token'];
        $isEmpty = false;
        foreach ($requiredFields as $field) {
            if (empty($data[$field])) {
                $isEmpty = true;
                break; // 一旦发现空值，退出循环
            }
        }
        if($isEmpty){
            exitjson(-1,'必要参数不可为空！');
        }
        $usertoken = $this->decode_token($data['token']);
        if(empty($usertoken['pid'])){
            exitjson(-1,'token信息错误！');
        }
        $userrow = $this->db->userrow($usertoken['pid'],'pid',"`pid`,`key`,`user`,`email`");
       
        if(empty($userrow) || $userrow['key'] != $usertoken['key']){
            exitjson(-1,'用户不存在');
        }
        $time      = time();
        $srow = $this->db->select('order',"*",['status'=>0,'price'=>$data['money'] , 'outtime'=>['operator'=>'gt','value'=>$time] , 'pid'=>$userrow['pid'] , 'type'=>$data['type']],['single_row'=>true,'limit'=>1])['result'];
        if(!empty($srow) && $srow['status'] == 0){
            $typename = array(
                'wxpay'=>'微信APP挂机',
                'alipay'=>'支付宝APP挂机'
            );
            $this->get_notify($typename , $srow , $userrow);
            exitjson(200,'success');
        }
        exitjson(-1,'无匹配金额！');
    }
    
}
?>